package com.model
{
	import com.vo.CarVo;
	import com.vo.KindVo;
	import com.vo.MeshVo;
	import com.vo.PartVo;
	import com.vo.SectionVo;
	import com.vo.TypeVo;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;

	/**
	 * 静态数据管理器
	 */	
	public class CarDataRemote
	{
		
		[Bindable]
		private static var sectionList:ArrayCollection;
		public static function getSectionList():ArrayCollection
		{
			if(sectionList == null){
				sectionList = new ArrayCollection();
				MenuXMLData.getMenuTitle(onTitle);
			}
			return sectionList;
		}
		private static function onTitle(index:int,title:String,name:String):void
		{
			sectionList.addItem(new SectionVo(index,title,name));
		}
		
		public static function getSectionVo(index:int):SectionVo{
			return sectionList.getItemAt(index) as SectionVo;
		}
		/**
		 * 检查车类型是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasKindEName(eName:String):Boolean{
			for (var i:int = kindList.length - 1; i >= 0; i--) 
			{
				var kvo:KindVo = kindList.getItemAt(i) as KindVo;
				if(kvo.eName == eName)return true;
			}
			return false;
		}
		/**
		 * 存在的情况下就不必向服务器发送请求
		 * @return 
		 */		
		public static function get hasKind():Boolean{
			return kindList != null && kindList.length != 0;
		}
		
		[Bindable]
		public static var kindList:ArrayCollection = new ArrayCollection();
//		kindList:Vector.<KindVo> = new Vector.<KindVo>();
		
		public static function addKindVo(obj:Object):void{
			var kvo:KindVo = new KindVo();
			for (var key:String in obj) 
			{
				kvo[key] = obj[key];
			}
			kindList.addItem(kvo);
		}
		
		public static function updateKindVo(obj:Object):void{
			var kvo:KindVo = getKindVo(obj.kindID);
			for (var key:String in obj) 
			{
				kvo[key] = obj[key];
			}
			kindList.refresh();//重新刷新视图
		}
		
		public static function getKindVo(id:int):KindVo{
			for (var i:int = kindList.length - 1; i >= 0; i--) 
			{
				var kvo:KindVo = kindList.getItemAt(i) as KindVo;
				if(kvo != null && kvo.kindID == id){
					return kvo;
				}
			}
			return null;
		}
		
		public static function deleteKindVo(id:int):void{
			for (var i:int = kindList.length - 1; i >= 0; i--) 
			{
				var kvo:KindVo = kindList.getItemAt(i) as KindVo;
				if(kvo != null && kvo.kindID == id){
					kindList.removeItemAt(i);
					break;
				}
			}
		}
		/**
		 * 检查车系别是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasTypeEName(kindID:int,eName:String):Boolean{
			var typeList:ArrayCollection = getTypeList(kindID);
			for (var i:int = typeList.length - 1; i >= 0; i--) 
			{
				var tvo:TypeVo = typeList.getItemAt(i) as TypeVo;
				if(tvo.eName == eName)return true;
			}
			return false;
		}
		/**
		 * 获取相对kindID的车系数组
		 * @param kindID
		 * @return 
		 */		
		public static function getTypeList(kindID:int):ArrayCollection{
			if(typeDic[kindID] == null)typeDic[kindID] = new ArrayCollection();
			return typeDic[kindID];
		}
		
//		[Bindable]
		/** 车系数组 */
//		private static var typeList:ArrayCollection = new ArrayCollection();
		private static var typeDic:Dictionary = new Dictionary(true);
		
		public static function addTypeVo(obj:Object):void{
			var typeList:ArrayCollection = getTypeList(obj.kindID);
			var tvo:TypeVo = new TypeVo();
			for (var key:String in obj) 
			{
				tvo[key] = obj[key];
			}
			typeList.addItem(tvo);
		}
		
		public static function updateTypeVo(obj:Object):void{
			var typeList:ArrayCollection = getTypeList(obj.kindID);
			var tvo:TypeVo = getTypeVo(obj.typeID,obj.kindID);
			for (var key:String in obj) 
			{
				tvo[key] = obj[key];
			}
			typeList.refresh();//重新刷新视图
		}
		public static function getTypeVo(id:int,kindID:int):TypeVo{
			var typeList:ArrayCollection = getTypeList(kindID);
			for (var i:int = typeList.length - 1; i >= 0; i--) 
			{
				var tvo:TypeVo = typeList.getItemAt(i) as TypeVo;
				if(tvo != null && tvo.typeID == id){
					return tvo;
				}
			}
			return null;
		}
		public static function deleteTypeVo(id:int,kindID:int):void{
			var typeList:ArrayCollection = getTypeList(kindID);
			for (var i:int = typeList.length - 1; i >= 0; i--) 
			{
				var tvo:TypeVo = typeList.getItemAt(i) as TypeVo;
				if(tvo != null && tvo.typeID == id){
					typeList.removeItemAt(i);
					break;
				}
			}
		}
		/**
		 * 检查车品牌是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasCarEName(eName:String):Boolean{
			for (var i:int = carList.length - 1; i >= 0; i--) 
			{
				var cvo:CarVo = carList.getItemAt(i) as CarVo;
				if(cvo.eName == eName)return true;
			}
			return false;
		}
		[Bindable]
		public static var carList:ArrayCollection = new ArrayCollection();
		public static function addCarVo(obj:Object):void{
			var cvo:CarVo = new CarVo();
			for (var key:String in obj) 
			{
				cvo[key] = obj[key];
			}
			carList.addItem(cvo);
		}
		public static function updateCarVo(obj:Object):void{
			var cvo:CarVo = getCarVo(obj.carID);
			for (var key:String in obj) 
			{
				cvo[key] = obj[key];
			}
			carList.refresh();//重新刷新视图
		}
		public static function getCarVo(id:int):CarVo{
			for (var i:int = carList.length - 1; i >= 0; i--) 
			{
				var cvo:CarVo = carList.getItemAt(i) as CarVo;
				if(cvo != null && cvo.carID == id){
					return cvo;
				}
			}
			return null;
		}
		public static function deleteCarVo(id:int):void{
			for (var i:int = carList.length - 1; i >= 0; i--) 
			{
				var cvo:CarVo = carList.getItemAt(i) as CarVo;
				if(cvo != null && cvo.carID == id){
					carList.removeItemAt(i);
					break;
				}
			}
		}
		/**
		 * 检查车部件是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasPartEName(eName:String,
											carID:int,sectionID:int,itemID:String):Boolean{
			var partList:ArrayCollection = getPartList(carID,sectionID,itemID);
			for (var i:int = partList.length - 1; i >= 0; i--) 
			{
				var pvo:PartVo = partList.getItemAt(i) as PartVo;
				if(pvo.eName == eName)return true;
			}
			return false;
		}
		/**
		 * 部件的三要素:车品牌 部件索引 部件条目索引 
		 */		
		private static var partDic:Dictionary = new Dictionary(true);
		/**
		 * 获取相对kindID的车系数组
		 * @param kindID
		 * @return 
		 */		
		public static function getPartList(carID:int,sectionID:int,itemID:String):ArrayCollection{
			if(itemID == null)return null;//说明没有选中条目
			if(partDic[carID + '_' + sectionID + '_' + itemID] == null)
				partDic[carID + '_' + sectionID + '_' + itemID] = new ArrayCollection();
			return partDic[carID + '_' + sectionID + '_' + itemID];
		}
		public static function addPartVo(obj:Object):PartVo{
			var pvo:PartVo = new PartVo();
			var partList:ArrayCollection = getPartList(obj.carID,obj.sectionID,obj.itemID);
			for (var key:String in obj) 
			{
				if(key == 'textureList'){
					if(obj[key] != null && obj[key] != '')
					pvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
					//保存图片数组
				}else{
					pvo[key] = obj[key];
				}
			}
			partList.addItem(pvo);
			return pvo;
		}
		public static function updatePartVo(obj:Object):PartVo{
			var partList:ArrayCollection = getPartList(obj.carID,obj.sectionID,obj.itemID);
			var pvo:PartVo = getPartVo(obj.partID,obj.carID,obj.sectionID,obj.itemID);
			for (var key:String in obj) 
			{
				if(key == 'textureList'){
					if(obj[key] != null && obj[key] != '')
					pvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
					//保存图片数组
				}else{
					pvo[key] = obj[key];
				}
			}
			partList.refresh();
			return pvo;
		}
		public static function getPartVo(partID:int,carID:int,sectionID:int,itemID:String):PartVo{
			var partList:ArrayCollection = getPartList(carID,sectionID,itemID);
			for (var i:int = partList.length - 1; i >= 0; i--) 
			{
				var pvo:PartVo = partList.getItemAt(i) as PartVo;
				if(pvo.partID == partID)return pvo;
			}
			return null;
		}
		public static function deletePartVo(partID:int,carID:int,sectionID:int,itemID:String):void{
			var partList:ArrayCollection = getPartList(carID,sectionID,itemID);
			for (var i:int = partList.length - 1; i >= 0; i--) 
			{
				var pvo:PartVo = partList.getItemAt(i) as PartVo;
				if(pvo != null && pvo.partID == partID){
					partList.removeItemAt(i);
					break;
				}
			}
		}
		/**
		 * 部件的三要素:车品牌 部件索引 部件条目索引 
		 */		
//		private static var meshDic:Dictionary = new Dictionary(true);
//		/**
//		 * 获取相对kindID的车系数组
//		 * @param kindID
//		 * @return 
//		 */		
//		public static function getMeshList(carID:int,sectionID:int,itemID:String):ArrayCollection{
//			if(itemID == null)return null;//说明没有选中条目
//			if(meshDic[carID + '_' + sectionID + '_' + itemID] == null)
//				meshDic[carID + '_' + sectionID + '_' + itemID] = new ArrayCollection();
//			return meshDic[carID + '_' + sectionID + '_' + itemID];
//		}
		public static function addMeshVo(obj:Object):MeshVo{
			var mvo:MeshVo = new MeshVo();
			for (var key:String in obj) 
			{
				if(key == 'textureList'){
					if(obj[key] != null && obj[key] != '')
					mvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
					//保存图片数组
				}else{
					mvo[key] = obj[key];
				}
			}
			var cvo:CarVo = getCarVo(obj.carID);
			cvo[obj.meshKey] = mvo;
			return mvo;
		}
		public static function updateMeshVo(obj:Object):MeshVo{
			var cvo:CarVo = getCarVo(obj.carID);
			var mvo:MeshVo = cvo[obj.meshKey];
			for (var key:String in obj) 
			{
				if(key == 'textureList'){
					if(obj[key] != null && obj[key] != '')
					mvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
					//保存图片数组
				}else{
					mvo[key] = obj[key];
				}
			}
			return mvo;
		}
	}
}